class ProductsController < ApplicationController
  before_filter :require_merchant_user,:only => [:new,:create,:edit,:update,:destroy,:create_giftcard]
  include UsersHelper
  # GET /products
  # GET /products.json
  def index
    @products = Product.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

  # GET /products/1
  # GET /products/1.json
  def show
    @product = Product.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @product }
    end
  end
  
  # GET /products/1
  # GET /products/1.json
  def confirm_card
  end

  # GET /products/new
  # GET /products/new.json
  def new    
    @product = Product.new  
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @product }
    end
  end
  def create_giftcard    

  end
  
  def edit_giftcard    
    @product = Product.find(params[:id])
  end
  
  def update_giftcard    
    @product = Product.find(params[:product_id])
    @product_condition = ProductCondition.find(params[:product_condition_id])
    @product.update_attributes(params[:product])
    @product_condition.update_attributes(params[:product_condition])
    redirect_to :action => 'preview_giftcard', :controller => 'products', :id => @product.id
  end
  
  def preview_giftcard    
    @product = Product.find(params[:id])
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(params[:product])
    @product.merchant = get_current_merchant()
    if(!params[:display_pic].nil?)
      orig_filename =  params[:display_pic].original_filename
      filename = SecureRandom.uuid + "_" + sanitize_filename(orig_filename)
      AWS::S3::S3Object.store(filename, params[:display_pic].read, PRODUCT_DISPLAY_IMAGE_BUCKET, :access => :public_read)
      url = AWS::S3::S3Object.url_for(filename, PRODUCT_DISPLAY_IMAGE_BUCKET, :authenticated => false)
      @product.image_url = url
    end
    respond_to do |format|
      if @product.save
        product_condition = ProductCondition.new
        product_condition.available_quantity = params[:available]
        product_condition.exp_date = params[:exp_date]
        product_condition.sold_quantity = 0
        product_condition.product = @product
        product_condition.save
        redirect_to :action => 'preview_giftcard', :controller => 'products', :id => @product.id
        return
      else
        format.html { render action: "new" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.json
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end
  
  
end
